from rest_framework.viewsets import ModelViewSet
from rest_framework import generics
from meiduo_admin.utils.pagination import MeiduoPagination
from meiduo_admin.serializers.skus import SkuSerializer,SkuSimpleSerializer
from goods.models import SKU
from django.db.models import Q
from rest_framework.decorators import action
from rest_framework.response import Response


class SkuView(ModelViewSet):
    def get_queryset(self):
        queryset = SKU.objects
        keyword = self.request.query_params.get('keyword')  ##？？ ‘’ query_params 是字典？
        if keyword:
            queryset = queryset.filter(Q(name__contains=keyword) | Q(caption__contains=keyword))
        queryset = queryset.order_by('-id')
        return queryset

    serializer_class = SkuSerializer
    pagination_class = MeiduoPagination

    ## 删除sku，不用再重写。因为设置了级联删除。
    @action(methods=['get'],detail=False)
    def simple(self,request):
        queryset=self.get_queryset()
        serializer=SkuSimpleSerializer(queryset,many=True)
        return Response(serializer.data)

# class SKUSimpleView(generics.ListAPIView):
#     queryset = SKU.objects.all()
#     serializer_class = SkuSimpleSerializer
